package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.flqy.baselibrary.utils.GlideHelper;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.AllCityLordBean;
import com.shenzhen.horselive.databinding.ActivityMainBinding;
import com.shenzhen.horselive.databinding.ActivityMapViewBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.IOverlay;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;

public class MapViewActivity extends AppBarActivity {

    ActivityMapViewBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMapViewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("地图预览");
        //初始化地图
        initViews();
    }

    TencentMap tencentMap;
    //初始化地图
    private void initViews() {
         tencentMap = binding.mapView.getMap();
        tencentMap.setOnMapLoadedCallback(new TencentMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
//                if (getIntent().getSerializableExtra("data")!=null)
//                {
//                    addMark();
//                }
                initData();
            }
        });

    }

    /**
     * 获取所有的城主
     */
    private void initData() {
        ApiFactory.getApiService().allMasterListAll(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<AllCityLordBean>(this) {
                    @Override
                    protected void onNextInActive(AllCityLordBean allCityLordBean) {
                        if (allCityLordBean.isSuccess())
                        {
                            addMark(allCityLordBean);
                        }
                        else
                        {
                            showToast(allCityLordBean.getErrMessage());
                        }
                    }
                });
    }

    //添加标记点
    private void addMark(AllCityLordBean allCityLordBean) {
        List<IOverlay>  list =  new ArrayList<>();
        for (int i = 0; i <allCityLordBean.getData().size() ; i++) {
            AllCityLordBean.DataBean  dataBean = allCityLordBean.getData().get(i);

          SimpleTarget<Bitmap>  simpleTarget=
                  new SimpleTarget<Bitmap>() {
                      @Override
                      public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                          //添加图标
                          View  view =  LayoutInflater.from(getContext()).inflate(R.layout.item_map_infowindows,null);
                          ImageView  imageView =  view.findViewById(R.id.img_user_head) ;
                          TextView  textViewName=  view.findViewById(R.id.tv_name);
                          TextView  textViewCity= view.findViewById(R.id.tv_city);
                          textViewName.setText(dataBean.getUserName());
                          textViewCity.setText(dataBean.getAreaName()+"城主");
                          imageView.setImageBitmap(resource);
                          BitmapDescriptor   bitmapDescriptor =  BitmapDescriptorFactory.fromView(view);
                          Marker marker = tencentMap.addMarker(new MarkerOptions(new LatLng(Double.parseDouble(dataBean.getLat()),Double.parseDouble(dataBean.getLng())))
                                  .icon(bitmapDescriptor)
                          ) ;
                          list.add(marker);
                      if (list.size()==allCityLordBean.getData().size())
                      {
                          //移动镜头显示所有mark
                          tencentMap.moveCamera(CameraUpdateFactory.newCameraPosition(
                                  tencentMap.calculateZoomToSpanLevel(
                                          list, null,
                                          0, 0, 0, 0)));

                      }
                      }
                  };
          //先加载才能添加地图坐标，傻逼地图
            Glide.with(this)
                    .asBitmap()
                    .load(dataBean.getHeadImg())
                    .centerCrop()
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .override(150, 150)
                    .into(simpleTarget);

        }


    }

    public static void start(Context context, AllCityLordBean allCityLordBean) {
        Intent starter = new Intent(context, MapViewActivity.class);
        starter.putExtra("data",allCityLordBean);
        context.startActivity(starter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        binding.mapView.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        binding.mapView.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        binding.mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        binding.mapView.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.mapView.onDestroy();
    }

}